package com.qinggeng.framework.scheduled;

import com.alibaba.fastjson.JSONObject;
import com.qinggeng.common.utils.CalendarAdjustUtils;
import com.qinggeng.project.system.domain.UserAccountConsumptionMonth;
import com.qinggeng.project.system.domain.UserAccountConsumptionYear;
import com.qinggeng.project.system.domain.UserActivity;
import com.qinggeng.project.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 吴鹏飞
 * @date 2020/11/19
 * spring定时任务
 */
@Slf4j
@Component
public class ScheduledService {
    @Autowired
    private IOrdersPointService ordersPointService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IGoodsKeepService goodsKeepService;

    @Autowired
    private IUserActivityService userActivityService;

    @Autowired
    private UserAccountConsumptionMonthService userAccountConsumptionMonthService;
    @Autowired
    private UserAccountConsumptionYearService userAccountConsumptionYearService;

    //默认红包分组
    public  static  final  String  NINE_REDE_NVELOPES_ACTIVITY_KEY="10000";

    public  static  final  String  NINETEEN_REDE_NVELOPES_ACTIVITY_KEY="20000";

    /*
        @Scheduled(cron = "0 10 22 * * *")
        //自提点销量统计
        public void pointSaleStatistics(){
            log.info("统计开始");
            boolean flag = ordersPointService.createPointSaleRecord();
            log.info("统计结束");
        }
    */

    @Scheduled(cron = "0 0 12 * * *")
    public void saleAutoAdd(){
        log.info("销量自增开始");
        JSONObject otherParams = JSONObject.parseObject(configService.selectConfigByKey("sys.goodsSale.add").toString());
        Integer number = Integer.parseInt(otherParams.getString("number"));
        goodsKeepService.updateGoodsKeepAutoAddSale(number);
        log.info("销量自增结束");
    }


    //9点运行一次
    @Scheduled(cron = "0 0 9 * * ?")
    public void openNineRedEnvelope(){
        userActivityService.updateStatusOpen(NINE_REDE_NVELOPES_ACTIVITY_KEY);

    }
    //10点运行一次
    @Scheduled(cron = "0 0 10 * * ?")
    public void closeNineRedEnvelope(){
        userActivityService.updateStatusClose(NINE_REDE_NVELOPES_ACTIVITY_KEY);

    }


    //19点运行一次
    @Scheduled(cron = "0 0 19 * * ?")
    public void openRedEnvelope(){
        userActivityService.updateStatusOpen(NINETEEN_REDE_NVELOPES_ACTIVITY_KEY);

    }
    //20 点运行一次
    @Scheduled(cron = "0 0 20 * * ?")
    public void closeRedEnvelope(){
        userActivityService.updateStatusClose(NINETEEN_REDE_NVELOPES_ACTIVITY_KEY);

    }


    //每月一号到7号进行 年统计,上个月未达到上上个月的不计入年消费
    //上个月消费金额 没有转零钱的 自动更新为0
    //@Scheduled(cron = "0 0 1 1 * ?")
    @Scheduled(cron = "0 0 12 * * *")
    public void userAccountConsumptionYear(){
        List<UserAccountConsumptionMonth> list=userAccountConsumptionMonthService.listThisMonthUserAccountConsumptionMonth();
        for (UserAccountConsumptionMonth month : list) {
            Long id=month.getId();
            if(month.getComputionYearStatus()!=0){
                continue;
            }
            BigDecimal lastMonthConsumption=month.getLastMonthConsumption();
            BigDecimal thisMonthConsumption=month.getThisMonthConsumption();
            if (thisMonthConsumption.compareTo(lastMonthConsumption)>=0){
                UserAccountConsumptionYear consumptionYear=new UserAccountConsumptionYear();
                consumptionYear.setUserId(month.getUserId());
                consumptionYear.setCreateYear(CalendarAdjustUtils.getYearToStr());
                consumptionYear.setThisYearConsumption(thisMonthConsumption);
                userAccountConsumptionYearService.addAccountConsumptionYear(consumptionYear);
                userAccountConsumptionMonthService.updateUserAccountYearConsumptionStatus(id);
            }

        }
    }


}
